package yaner.hotel.manager.resource.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;
import yaner.hotel.manager.common.dto.dict.DictAddDto;
import yaner.hotel.manager.common.dto.dict.DictDto;
import yaner.hotel.manager.common.dto.dict.DictUpdateDto;
import yaner.hotel.manager.common.vo.DictVo;
import yaner.hotel.manager.resource.dao.IDictDao;
import yaner.hotel.manager.resource.entity.Dict;
import yaner.hotel.manager.resource.mapper.IDictMapper;
import yaner.hotel.manager.resource.service.IDictService;

import java.util.ArrayList;
import java.util.List;

@Service
public class DictServiceImpl implements IDictService {
    private final IDictDao dictDao;
    private final IDictMapper dictMapper;
    public DictServiceImpl(IDictDao dictDao, IDictMapper dictMapper) {
        this.dictDao = dictDao;
        this.dictMapper = dictMapper;
    }

    @Override
    public List<DictVo> dictList(DictDto dictDto) {
        List<DictVo> dictVoList = new ArrayList<>();
        List<Dict> dictList = dictDao.selectList(
                new QueryWrapper<Dict>().lambda().orderByDesc(Dict::getCreateTime)
        );
        dictList.forEach(dict -> {
            DictVo vo = dictMapper.toVo(dict);
            dictVoList.add(vo);
        });
        return dictVoList;
    }

    @Override
    public DictVo getDictInfo(String dictId) {
        Dict dict = dictDao.selectById(dictId);
        return dictMapper.toVo(dict);
    }

    @Override
    public String addDictInfo(DictAddDto dictAddDto) {
        Dict dict = dictMapper.toAddEntity(dictAddDto);
        int i = dictDao.insert(dict);
        return String.valueOf(i);
    }

    @Override
    public String updateDictInfo(DictUpdateDto dictUpdateDto) {
        Dict dict = dictMapper.toUpdateEntity(dictUpdateDto);
        int i = dictDao.updateById(dict);
        return String.valueOf(i);
    }

    @Override
    public String deleteDictInfo(String dictId) {
        int i = dictDao.deleteById(dictId);
        return String.valueOf(i);
    }
}
